#!/usr/bin/python
import yeast_util

infile = open('KruglyakGenewisePhenoProteomics_associations.tsv');
feature_helper = {}
feature_helper["IMP2"] = "C:GENO:IMP2:chrIX:53981:55021:-"
feature_helper["AAP1"] = "C:GENO:AAP1:chrVIII:198740:201310:-"
features_hash = {}
fshout = open('loadKruglyakFeaturesSql.sh','w')
outfile = open('/local/dudley/omics/KruglyakFeaturesOut.tsv','w')
fromPred = 0
fromTarget = 0
for line in infile:
	tokens = line.split('\t')
	if (tokens[0].find("'") != -1 or tokens[1].find("'") != -1):
		#print tokens[0] + "\t" + tokens[1]
		if (tokens[0].find('IMP2') != -1):
			tokens[0] = feature_helper.get("IMP2")
		if (tokens[1].find('IMP2') != -1):
			tokens[1] = feature_helper.get("IMP2")
		if (tokens[0].find('AAP1') != -1):
			tokens[0] = feature_helper.get("AAP1")	
		if (tokens[1].find('AAP1') != -1):
			tokens[1] = feature_helper.get("AAP1")
	if (tokens[0].find('PHENO') != -1):
		tokens[0] = yeast_util.transPhenoFeature(tokens[0])
	if (tokens[1].find('PHENO') != -1):
		tokens[1] = yeasat_util.transPhenoFeature(tokens[1])

	if (not features_hash.get(tokens[0])):
		features_hash[tokens[0]] = tokens[0]
		#tokenize feature alias and then add to tsv
		data = tokens[0].split(':')
		if len(data) > 4 and len(data[3]) > 3:
			data[3] = data[3][3:]
		outfile.write(tokens[0] + "\t" + "\t".join(data) + "\n")
		fromPred = fromPred + 1
	if (not features_hash.get(tokens[1])):
		features_hash[tokens[1]] = tokens[1]
		data = tokens[1].split(':')
		if len(data) > 4 and len(data[3]) > 3:
                        data[3] = data[3][3:]
                outfile.write(tokens[1] + "\t" + "\t".join(data) + "\n")
		fromTarget = fromTarget + 1			
infile.close()
outfile.close()
fshout.write("#!/bin/bash\n")
fshout.write("mysql --user=root --database=omics<<EOFMYSQL\n")
fshout.write("load data local infile '/local/dudley/omics/KruglyakFeaturesOut.tsv' replace INTO TABLE kruglyak_features " + " fields terminated by '\\t' LINES TERMINATED BY '\\n';")
fshout.write("\nEOFMYSQL")
fshout.close()
#print("from predictor %i from target %i" %(fromPred, fromTarget))
